/**
 * 
 */
package cn.gov.msa.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;

import cn.gov.msa.dao.DictionaryDetailMapper;
import cn.gov.msa.dao.DictionaryMapper;
import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.Dictionary;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.DictionaryService;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */
public class DictionaryServiceImpl implements DictionaryService {


	@Autowired
	private DictionaryMapper dictionaryMapper;
	@Autowired
	private DictionaryDetailMapper dictionaryDetailMapper;

	@Transactional
	public int deleteDictionary(String dictcode) throws Exception {

		int detail = dictionaryDetailMapper.deleteByDictCode(dictcode);
		
		int result = dictionaryMapper.deleteByPrimaryKey(dictcode);
		
		
		return result;
	}

	

	@Transactional
	public boolean saveData(Dictionary dictionary, List<DictionaryDetail> dictionaryDetailList) throws Exception {
		boolean result = false;
				
		int idict = dictionaryMapper.insert(dictionary);
		
		for (DictionaryDetail dictionaryDetail : dictionaryDetailList) {
			dictionaryDetail.setDictcode(dictionary.getDictcode());
			int idictDetail = dictionaryDetailMapper.insert(dictionaryDetail);
		}
		
		if(idict > 0)
			result = true;
		
		return result;
	}
	

	@Transactional
	public boolean updateData(Dictionary dictionary, List<DictionaryDetail> dictionaryDetailList) throws Exception {
		boolean result = false;
		
		int idict = dictionaryMapper.updateByPrimaryKey(dictionary);
		
		dictionaryDetailMapper.deleteByDictCode(dictionary.getDictcode());
		
		for (DictionaryDetail dictionaryDetail : dictionaryDetailList) {
			dictionaryDetail.setDictcode(dictionary.getDictcode());
			int idictDetail = dictionaryDetailMapper.insert(dictionaryDetail);
		}
		
		if(idict > 0)
			result = true;
		
		return result;
	}



	public List<Dictionary> selectDictionary(int pageNum, int pageSize, Dictionary dictionary) throws Exception {
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码  
		List<Dictionary> listDictionarys = dictionaryMapper.selectData(dictionary);
		
		return listDictionarys;
	}
	
	
	public Dictionary getInfoByDictCode(String dictcode) throws Exception {
        
        //读取数据库信息
	    Dictionary dictionary = dictionaryMapper.selectByPrimaryKey(dictcode);

	    return dictionary;
	}
}
